import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const store = new Vuex.Store({
  state:{
    HOST:'http://localhost:8887',
    loginIn:false,
    userId:'',  //用户id
    username:'',  //用户账号
    avatar:'',  //用户号头像
    collection:null, //用户收藏
    history:null,    //用户历史记录
    praise:null,    //用户点赞记录
    commentPraise:null,    //用户评论点赞记录
    isBarrage:true,    //弹幕开关
  },
  getters:{
    loginIn:state =>{
      let loginIn = state.loginIn
      if(!loginIn){
        loginIn = JSON.parse(window.localStorage.getItem('loginIn'))
      }
      return loginIn
    },
    userId:state =>{
      let userId = state.userId
      if(!userId){
        userId = JSON.parse(window.localStorage.getItem('userId'))
      }
      return userId
    },
    username: state => {
      let username = state.username
      if (!username) {
        username = JSON.parse(window.localStorage.getItem('username') || null)
      }
      return username
    },
    avatar: state => {
      let avatar = state.avatar
      if (!avatar) {
        avatar = JSON.parse(window.localStorage.getItem('avatar') || null)
      }
      return avatar
    },
    collection: state => {
      let collection = state.collection
      if (!collection) {
        collection = JSON.parse(window.localStorage.getItem('collection') || null)
      }
      return collection
    },
    history: state => {
      let history = state.history
      if (!history) {
        history = JSON.parse(window.localStorage.getItem('history') || null)
      }
      return history
    },
    praise: state => {
      let praise = state.praise
      if (!praise) {
        praise = JSON.parse(window.localStorage.getItem('praise') || null)
      }
      return praise
    },
    commentPraise: state => {
      let commentPraise = state.commentPraise
      if (!commentPraise) {
        commentPraise = JSON.parse(window.localStorage.getItem('commentPraise') || null)
      }
      return commentPraise
    },
    isBarrage: state => {
      let isBarrage = state.isBarrage
      if (!isBarrage) {
        isBarrage = JSON.parse(window.localStorage.getItem('isBarrage') || null)
      }
      return isBarrage
    },
  },
  mutations:{
    setLoginIn:(state,loginIn) =>{
      state.loginIn = loginIn,
      window.localStorage.setItem('loginIn',JSON.stringify(loginIn))
    },
    setUserId:(state,userId) =>{
      state.userId = userId,
      window.localStorage.setItem('userId',JSON.stringify(userId))
    },
    setUsername:(state,username) =>{
      state.username = username,
      window.localStorage.setItem('username',JSON.stringify(username))
    },
    setAvatar:(state,avatar) =>{
      state.avatar = avatar,
      window.localStorage.setItem('avatar',JSON.stringify(avatar))
    },
    setCollection:(state,collection) =>{
      state.collection = collection,
      window.localStorage.setItem('collection',JSON.stringify(collection))
    },
    setHistory:(state,history) =>{
      state.history = history,
      window.localStorage.setItem('history',JSON.stringify(history))
    },
    setPraise:(state,praise) =>{
      state.praise = praise,
      window.localStorage.setItem('praise',JSON.stringify(praise))
    },
    setCommentPraise:(state,commentPraise) =>{
      state.commentPraise = commentPraise,
      window.localStorage.setItem('commentPraise',JSON.stringify(commentPraise))
    },
    setIsBarrage:(state,isBarrage) =>{
      state.isBarrage = isBarrage,
      window.localStorage.setItem('isBarrage',JSON.stringify(isBarrage))
    },
  }
})

export default store